package com.oyc.demo.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.fastjson.JSONObject;
import com.oyc.demo.domain.Cablebasic;
import com.oyc.demo.exception.BaseBusinessException;
import com.oyc.demo.listener.CheckListener;
import com.oyc.demo.listener.TemimportListener;
import com.oyc.demo.service.ExporterrorService;
import com.oyc.demo.service.TemimportService;
import com.oyc.demo.util.Constants;
import com.oyc.demo.util.Result;

import io.swagger.annotations.ApiOperation;

/**
 * (Cablebasic)表控制层
 *
 * @author 杜学森
 * @since 2021-01-20 18:51:53
 */
@RestController
@RequestMapping("/temimport")
public class TemimportController {
    /**
     * 服务对象
     */
    @Autowired
    private TemimportService temimportService;
    
    /**
     * 服务对象
     */
    @Autowired
    private ExporterrorService exporterrorService;
   

    @RequestMapping(value="/importexcel",method= RequestMethod.POST)
    @ApiOperation("导入")
    public Result<JSONObject> importexcel(HttpServletRequest request, @RequestParam("files") MultipartFile[] files) throws IllegalStateException, IOException {
    	 try {
    		 
        	//删除临时表和错误表数据
            exporterrorService.clear();
            
            if(files!=null&&files.length>0){
            	for (int i = 0; i < files.length; i++) {
               	 MultipartFile file=files[i];
               	 
               	 if (file.isEmpty()) {
               		 continue;
               	 }
               	 if(!file.getOriginalFilename().toLowerCase().endsWith(".xls")&&!file.getOriginalFilename().toLowerCase().endsWith(".xlsx")){
               		 continue;
               	 }
               	 
               	 
               	 String fileName="";
                    //可以远程上传文件
                    if(Constants.debugmodel){
                   	//判断上传路径是否存在
                   	 File savefile = new File(Constants.importpath);
                        if (!savefile.exists()) {
                           savefile.mkdirs();
                        }
                   	  //生成随机文件名    	 
                   	 fileName = Constants.importpath +UUID.randomUUID().toString()+".xlsx";
                        File dest = new File(fileName);
                        file.transferTo(dest);
                    }else{
                   	 fileName=file.getOriginalFilename();
                    }
                    
                    //模板检查
                    EasyExcel.read(fileName, Cablebasic.class, new CheckListener()).sheet().headRowNumber(3).doRead();
                    //最终导入数据
                    EasyExcel.read(fileName, Cablebasic.class, new TemimportListener(temimportService,exporterrorService,file.getOriginalFilename())).sheet().headRowNumber(3).doRead();
            	}
            	 //进行计算
                JSONObject obj=exporterrorService.calculate();
                
                return Result.success(obj);
            }
            return Result.error("导入失败,请选择正确的模板文件后再执行导入操作",-1);
    	 } catch (ExcelAnalysisException e) {
    		 e.printStackTrace();
 			return Result.error(e.getLocalizedMessage(), 500);
 		 } catch (IllegalStateException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 			return Result.error("导入失败", 500);
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 			return Result.error("导入失败", 500);
 		} 

    }
}